package com.wjr.dao;

import com.wjr.bean.User;

import java.sql.*;
import java.util.List;

/**
 * @Author: wjr
 * @Description:
 * @Date Created in  2020-09-01 14:06
 * @Modified By:
 */
public class MySqlDao implements BaseUserDao{
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public Connection connectionMysql(){
        String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";
        String username = "root";
        String password = "123";
        Connection conn = null;
        //建立连接
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }

    @Override
    public boolean findUser(User user) {
        Connection conn = connectionMysql();
        Statement state = null;
        ResultSet rs = null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery("select * from user where " +
                    "username='" + user.getUserName() + "'and password='" + user.getPassWord() + "'");
            //如果游标可以向下移动则表示查询成功
            return rs.next();
        } catch (SQLException throwables) {
            System.out.println("连接失败");
            throwables.printStackTrace();
        }finally {
            //资源释放
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                state.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean insertUser(User user) {
        return false;
    }

    @Override
    public List<User> findAll(User user) {
        return null;
    }
}
